package think.in.spring.ioc.overview.dependency.injection;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import think.in.spring.ioc.overview.annotation.Super;
import think.in.spring.ioc.overview.domain.User;
import think.in.spring.ioc.overview.repository.UserRepository;

import java.util.Collection;
import java.util.Map;

/**
 * 依赖查找
 */
public class DependencyInjectionDemo {
    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(
                "classpath:META-INF/dependency-injection-context.xml");
//        UserRepository userRepository = (UserRepository) beanFactory.getBean("userRepository");
//        System.out.println(userRepository.getUsers());
//        System.out.println(userRepository.getBeanFactory());

        UserRepository userRepositoryByAutowired = (UserRepository) beanFactory.getBean("userRepositoryByAutowired");
        System.out.println("Repository的:"+ userRepositoryByAutowired.getBeanFactory());
        System.out.println("main方法的："+beanFactory);
        System.out.println("main方法的和Repository对比:"+beanFactory.equals(userRepositoryByAutowired.getBeanFactory()));
        ApplicationContext context = userRepositoryByAutowired.getContext();
        System.out.println("context:"+context);
//        User user = userRepositoryByAutowired.getUserObjectFactory().getObject();//由于superUser设置primary，获取优先获取它
//        System.out.println(user);
        //这里获取context和beanFactory是一个对象
        ObjectFactory<ApplicationContext> contextObjectFactory = userRepositoryByAutowired.getContextObjectFactory();
        System.out.println("contextObjectFactory:"+contextObjectFactory);
        ApplicationContext context1 = contextObjectFactory.getObject();
        System.out.println(context1);


    }


}
